﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Shapes;

namespace Merise_designer.Methodes
{
    static class CL_Graphique
    {

        //Retourne le premier control qui contient le curseur
        public static Control GetActiveUIElement(Point cursorPos, UIElementCollection collection)
        {
            Control ctl = null;

            //explore les éléments Control de la collection
            foreach (Control child in collection.OfType<Control>())
            {
                //ne scanne que les objets commançant par le préfixe du nom automatique
                if (child.Name.Count() > 0 && child.Name.Substring(0, CL_Variables.getPrefixNameAuto.Length) == CL_Variables.getPrefixNameAuto)
                {
                    //cherche l'objet sous le pointeur de souris courante
                    if (IsCursorInMarge(cursorPos, child.Margin, child.Height, child.Width))
                    {
                        ctl = child;
                        return ctl;
                    }
                }
            }

            //Explorer les titres
            foreach (TextBlock child in collection.OfType<TextBlock>())
            {
                Console.WriteLine(child.Name);
                //ne scanne que les objets commançant par le préfixe du nom automatique
                if (child.Name.Count()>0 && child.Name.Substring(0, CL_Variables.getTitleName().Length) == CL_Variables.getTitleName())
                {
                    //cherche l'objet sous le pointeur de souris courante
                    if (IsCursorInMarge(cursorPos, child.Margin, child.Height, child.Width))
                    {

                        //on recherche l'entité ou l'asso qui correspond à ce titre
                        string nameObj = child.Name.Replace(CL_Variables.getTitleName(), "");
                        foreach (Control c in collection.OfType<Control>())
                        {
                            if (c.Name == nameObj)
                            {
                                ctl = c;
                                return ctl;
                            }
                        }
                    }
                }
            }

            return ctl;
        }

        //Vérifie si le curseur est dans l'objet
        private static bool IsCursorInMarge(Point cursorPos, Thickness marge, Double haut, Double larg)
        {
            if (cursorPos.X >= marge.Left & cursorPos.X <= marge.Left + larg)
            {
                if (cursorPos.Y >= marge.Top & cursorPos.Y <= marge.Top + haut)
                {
                    return true;
                }
            }
            return false;
        }

        //Retourne le premier control qui est contenu dans le rectangle de selection
        public static List<Control> GetActiveUIElement(Rectangle rect, UIElementCollection collection)
        {
            List<Control> result = new List<Control>();

            //explore les éléments Control de la collection
            foreach (Control child in collection.OfType<Control>())
            {
                //ne scanne que les objets commançant par le préfixe du nom automatique
                if (child.Name.Count() > 0 && child.Name.Substring(0, CL_Variables.getPrefixNameAuto.Length) == CL_Variables.getPrefixNameAuto)
                {
                    //cherche l'objet dans le rectangle de selection
                    if (IsCursorInRect(new Point(child.Margin.Left+(child.Width/2),child.Margin.Top+(child.Height/2)), rect))
                    {
                        result.Add(child);
                    }
                }
            }
            return result;
        }

        //Vérifie si le point central est dans le rectangle
        private static bool IsCursorInRect(Point center, Rectangle rect)
        {
            if ((center.X >= rect.Margin.Left & center.X <= rect.Margin.Left + rect.Width) & (center.Y >= rect.Margin.Top & center.Y <= rect.Margin.Top+rect.Height)) return true;
            return false;
        }

        /// <summary>
        /// Nettoye les poignées résiduelles
        /// </summary>
        /// <param name="collection"></param>
        public static void ClearPoigneesResiduelles(Grid grille)
        {
            //explore les éléments Control de la collection
            List<Rectangle> rectToDelete = new List<Rectangle>();
            foreach (Rectangle child in grille.Children.OfType<Rectangle>())
            {
                //ne scanne que les objets commançant par le préfixe du nom des poignées
                if (child.Name.Length > Configuration.namePoignee.Length && child.Name.Substring(0, Configuration.namePoignee.Length) == Configuration.namePoignee.ToString())
                {
                    rectToDelete.Add(child);
                }
            }

            foreach (Rectangle r in rectToDelete)
            {
                grille.Children.Remove(r);
            }
        }

    }
}
